/**
 ******************************************************************************
 * @file           : custom_bus.h
 * @brief          : header file for the BSP BUS IO driver
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _CUSTOM_BUS_H_
#define _CUSTOM_BUS_H_

#ifdef __cplusplus
extern "C"
{
#endif

/* Includes ------------------------------------------------------------------*/
#include <Arduino.h>
#include "xz_config.h"
#include "custom_errno.h"

    /** @addtogroup BSP
     * @{
     */

    /** @addtogroup CUSTOM
     * @{
     */

#define NFC04A1_I2C_Init BSP_I2C1_Init
#define NFC04A1_I2C_DeInit BSP_I2C1_DeInit
#define NFC04A1_I2C_ReadReg16 BSP_I2C1_ReadReg16
#define NFC04A1_I2C_WriteReg16 BSP_I2C1_WriteReg16
#define NFC04A1_I2C_Recv BSP_I2C1_Recv
#define NFC04A1_I2C_IsReady BSP_I2C1_IsReady

#define NFC04A1_GetTick HAL_GetTick

    /** @defgroup CUSTOM_BUS CUSTOM BUS
     * @{
     */

    /** @defgroup CUSTOM_BUS_Exported_Constants CUSTOM BUS Exported Constants
     * @{
     */

#define GPIO_PORT_CLK_ENABLE(x)           \
    do                                    \
    {                                     \
        if (x == GPIOA)                   \
        {                                 \
            __HAL_RCC_GPIOA_CLK_ENABLE(); \
        }                                 \
        else if (x == GPIOB)              \
        {                                 \
            __HAL_RCC_GPIOB_CLK_ENABLE(); \
        }                                 \
        else if (x == GPIOC)              \
        {                                 \
            __HAL_RCC_GPIOC_CLK_ENABLE(); \
        }                                 \
    } while (0)


#define BUS_I2C1_SCL_GPIO_PIN digitalPinToBitMask(I2C1_SCL)
#define BUS_I2C1_SCL_GPIO_PORT digitalPinToPort(I2C1_SCL)
#define BUS_I2C1_SCL_GPIO_CLK_ENABLE() GPIO_PORT_CLK_ENABLE(BUS_I2C1_SCL_GPIO_PORT)

#define BUS_I2C1_SDA_GPIO_PIN digitalPinToBitMask(I2C1_SDA)
#define BUS_I2C1_SDA_GPIO_PORT digitalPinToPort(I2C1_SDA)
#define BUS_I2C1_SDA_GPIO_CLK_ENABLE() GPIO_PORT_CLK_ENABLE(BUS_I2C1_SDA_GPIO_PORT)

#ifndef BUS_I2C1_POLL_TIMEOUT
#define BUS_I2C1_POLL_TIMEOUT 0x1000U
#endif


/**
 * @}
 */

/** @defgroup CUSTOM_BUS_Private_Types CUSTOM BUS Private types
 * @{
 */
#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1U)
    typedef struct
    {
        pI2C_CallbackTypeDef pMspInitCb;
        pI2C_CallbackTypeDef pMspDeInitCb;
    } BSP_I2C_Cb_t;
#endif /* (USE_HAL_I2C_REGISTER_CALLBACKS == 1U) */
    /**
     * @}
     */

    /** @defgroup CUSTOM_LOW_LEVEL_Exported_Variables LOW LEVEL Exported Constants
     * @{
     */

    extern I2C_HandleTypeDef hi2c1;

    /**
     * @}
     */

    /** @addtogroup CUSTOM_BUS_Exported_Functions
     * @{
     */

    /* BUS IO driver over I2C Peripheral */
    HAL_StatusTypeDef MX_I2C1_Init(I2C_HandleTypeDef *hi2c);
    int32_t BSP_I2C1_Init(void);
    int32_t BSP_I2C1_DeInit(void);
    int32_t BSP_I2C1_IsReady(uint16_t DevAddr, uint32_t Trials);
    int32_t BSP_I2C1_WriteReg(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
    int32_t BSP_I2C1_ReadReg(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
    int32_t BSP_I2C1_WriteReg16(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
    int32_t BSP_I2C1_ReadReg16(uint16_t Addr, uint16_t Reg, uint8_t *pData, uint16_t Length);
    int32_t BSP_I2C1_Send(uint16_t DevAddr, uint8_t *pData, uint16_t Length);
    int32_t BSP_I2C1_Recv(uint16_t DevAddr, uint8_t *pData, uint16_t Length);
    int32_t BSP_I2C1_SendRecv(uint16_t DevAddr, uint8_t *pTxdata, uint8_t *pRxdata, uint16_t Length);
#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1U)
    int32_t BSP_I2C1_RegisterDefaultMspCallbacks(void);
    int32_t BSP_I2C1_RegisterMspCallbacks(BSP_I2C_Cb_t *Callbacks);
#endif /* (USE_HAL_I2C_REGISTER_CALLBACKS == 1U) */

    int32_t BSP_GetTick(void);

/**
 * @}
 */
#define NFC04A1_NFCTAG_INSTANCE (0)
/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */
#ifdef __cplusplus
}
#endif

#endif /* CUSTOM_BUS_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
